package ld.web.listener;

import ld.pojo.Product;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import java.util.HashMap;

/**
 * @Author: Liudao
 * @Date: 2022-08-02-15:49
 * @Description:
 */
@WebListener
public class Applicationlistener implements ServletContextListener, ServletContextAttributeListener {

    //下面两个是对对象的监控
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        HashMap<Integer, Product> map = new HashMap<>();
        map.put(1, new Product(1,"旺仔牛奶",5.5,500));
        map.put(2, new Product(2,"飞鸽自行车",1500,100));
        map.put(3, new Product(3,"步步高学习机",2550,180));
        map.put(4, new Product(4,"全自动滚筒洗衣机",6000,80));
        map.put(5, new Product(5,"康师傅方便面",2.5,20000));
        map.put(6, new Product(6,"戴尔笔记本",4888,20));
        map.put(7, new Product(7,"海贼王手机壳",15,8800));
        map.put(8, new Product(8,"粉色保温杯",20,1000));
        map.put(9, new Product(9,"小型无人机",8888,10));
        map.put(10, new Product(10,"格力空调",6888,40));

        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("products",map);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }

    //下面三个方法是对application属性的监控
    @Override
    public void attributeAdded(ServletContextAttributeEvent servletContextAttributeEvent) {}

    @Override
    public void attributeRemoved(ServletContextAttributeEvent servletContextAttributeEvent) {}

    @Override
    public void attributeReplaced(ServletContextAttributeEvent servletContextAttributeEvent) {}
}
